package bb.lanxing.adapter.sport;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import bb.lanxing.R;
import bb.lanxing.utils.PanelItemTypeUtil;

public class DevicePanelSettingTypeAdapter extends RecyclerView.Adapter<DevicePanelSettingTypeAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListener listener;

    private int mBigType;
    private int[] typeList;
    private int mSelectedTab = -1;
    private int mSelectedItem = -1;

    public DevicePanelSettingTypeAdapter(int bigType, int[] types) {
        mBigType = bigType;
        typeList = types;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_sport_new_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (typeList != null) {
            int type = typeList[position];
            if (mBigType == mSelectedTab && position == mSelectedItem) {
                holder.layout.setForeground(mContext.getDrawable(R.drawable.item_selected_fg));
            } else {
                holder.layout.setForeground(null);
            }
            holder.sport_item_digital.setText(PanelItemTypeUtil.resetDeviceDefault(type));
            holder.sport_item_digital_unit.setText(PanelItemTypeUtil.getDeviceItemName(type));
            holder.itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (typeList != null) {
            return typeList.length;
        }
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void updateSelectedItem(int selectedTab, int selectedItem) {
        mSelectedTab = selectedTab;
        mSelectedItem = selectedItem;
        notifyDataSetChanged();
    }

    public void removeUnselectedItem() {
        mSelectedTab = -1;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layout;
        TextView sport_item_digital;
        TextView sport_item_digital_unit;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = itemView.findViewById(R.id.layout);
            sport_item_digital = itemView.findViewById(R.id.sport_item_digital);
            sport_item_digital_unit = itemView.findViewById(R.id.sport_item_digital_unit);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}